
package com.asdan.seededu.util;  
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

 
/**
 * 
 * @author Qixuan.Chen
 */
public class SendEmail {

    public static final String HOST = "smtp.qq.com";
    public static final String PROTOCOL = "smtp";   
    public static final int PORT = 465;  
    public static final String FROM = "304069676@qq.com";//发件人的email
    public static final String PWD = "8f0618b56zry!";//发件人密码

    public static void send(String toEmail , String content) {
    System.out.println("--send--"+content);
    Properties props = new Properties();  
    // 开启debug调试  
    props.setProperty("mail.debug", "true");  
    // 发送服务器需要身份验证  
    props.setProperty("mail.smtp.auth", "true");  
    // 设置邮件服务器主机名  
    props.setProperty("mail.host", HOST);  
    // 发送邮件协议名称  
    props.setProperty("mail.transport.protocol", PROTOCOL);  
      
    // 设置环境信息  
    Session session = Session.getInstance(props, new Authenticator() {  
        // 在session中设置账户信息，Transport发送邮件时会使用  
        protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(FROM, PWD);  
        }  
    });  
    Message msg = new MimeMessage(session);  
    try {
		msg.setFrom(new InternetAddress(FROM));
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(toEmail));  
	    msg.setSubject("Seededu账号激活邮件");  
	    msg.setContent(content, "text/html;charset=UTF-8");  
	    Transport.send(msg);  
	} catch (AddressException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (MessagingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}  
    
 }
}
 